$(function(){
	//加载按钮绑定的事件
	page.bind();
	//加载生产线
	page.getProductionLine('1');
	page.getMaterialType();
	page.removeBtnBind();
	page.getAdminWorkshop();
	queryCycle('1');
});
var processingObject = "";
var page = {
		//给按钮绑定事件
		bind:function(){
			$("#import").bind("click",function() {
				$("#importWorkshopModal").modal("show");
			});
			//点击导入车间模态框的确定按钮
			$("#addModalBtn2").bind("click",function(){
				var workshopStr = "";
				$(".right-box2 li.item").each(function(){
					workshopStr+=$(this).attr("value")+",";
				});	
				workshopStr = workshopStr.substring(0,workshopStr.length-1);
				if(workshopStr.length<=0){
					layAlert("没有选择要导入的新车间");
					return false;
				}
				ajaxUtil.ajaxRuquest("basicData/importWorkshop",
						{'workshopStr':workshopStr},
						function(data){
					layAlert(data);
				});
				page.getProductionLine('1');
				page.getAdminWorkshop();
				queryCycle($("#view").val());
				$("#importWorkshopModal").modal("hide");
				$(".right-box2").html("");
			});
			//点击切换查看状态按钮
			$("#view").bind("click",function(){
				var view = $("#view").val();
				if(view=="1"){
					view = "0";
				}else{
					view = "1";
				}
				queryCycle(view);
				$("#view").val(view);
			});
			//模态框显示
			$("#addProcessingObjectModal").on("show.bs.modal",function(){
				$("#processingObject .pad").each(function(){
					$(".right-box").append($(".left-box").find('[value="'+$(this).attr("value")+'"]'));
				});
			});
			//模态框隐藏
			$("#addProcessingObjectModal").on("hidden.bs.modal",function(){
				$(".left-box").append($(".right-box").find('.item').removeClass('selected-item'));
			});
			//选择加工对象模态框确定按钮
			$("#addModalBtn").bind("click",function(){
				var html = "";
				$(".right-box li").each(function(){
					html+="<button class='btn btn-success radius pad' value='"+$(this).attr("value")+"'>"+$(this).attr("value")+"</button>&nbsp;";
				});	
				if(html.length>0){
					$("#processingObject").html("");
					$("#processingObject").html(html);
				}else{
					$("#processingObject").html("<button class='btn btn-primary radius'>选择加工对象</button>");
				}
				$("#addProcessingObjectModal").modal("hide");
			});
			//添加加工对象按钮,打开模态框
			$("#processingObject").bind("click",function(){
				$("#addProcessingObjectModal").modal("show");
			});
			//改变生产线时
			$("#productionLine").bind("change",function(){
				var productionLine = $("#productionLine").val();
				//加载车间名称
				page.getWorkshopName2(productionLine);
			});
			//改变车间名称时
			$("#workshopName").bind("change",function(){
				var id = $("#workshopName").val();
				if(id!="请选择"){
					page.getWorkshopProductionDetail(id);
				}
			});
			//点击+车间名称时
			$("#addWorkshopBtn").bind("click",function(){
				$("#addWorkshopTr").show("slow");
			});
			//点击添加车间名称按钮时
			$("#addWorkshopNameBtn").bind("click",function(){
				var productionLine = $("#productionLine option:selected").val();
				var addWorkshopName = $("#addWorkshopName").val();
				//如果没有输入车间名称
				if(addWorkshopName==""){
					layAlert("请输入车间名称");
					return false;
				}else{
					$("#addWorkshopTr").hide("slow");
					$("#workshopName").append("<option value='0'>"+addWorkshopName+"</option>");
					$("#workshopName option[value='0']").attr("selected","selected");
				}
			});
			//点击添加按钮时
			$("#insertBtn").bind("click",function(){
				var productionLine = $("#productionLine option:selected").text();
				if(productionLine=="请选择"){
					layAlert("请选择生产线");
					return false;
				}
				var workshopName = $("#workshopName option:selected").text();
				if(workshopName=="请选择"){
					layAlert("请选择车间名称");
					return false;
				}
				var status = $("#status").val();
				if(status=="请选择"){
					layAlert("请选择状态");
					return false;
				}
				processingObject = "";
				$("#processingObject .pad").each(function(){
					processingObject+=$(this).attr("value")+",";
				});
				processingObject = processingObject.substring(0,processingObject.length-1);
				var sort = $("#orderId").val();
				if(sort!="" && /^\d+$/.test(sort)==false){
					layer.msg("顺序只能是正整数");
					return false;
				}
				var temp1 = $("#processCycle").val();
				if(temp1!="" && /^\d+$/.test(temp1)==false){
					layer.msg("流程周期只能是正整数");
					return false;
				}
				var temp2 = $("#repairCycle").val();
				if(temp2!="" && /^\d+$/.test(temp2)==false){
					layer.msg("返修周期只能是正整数");
					return false;
				}
				var turnover = $("#turnover").val();
				if(turnover!="" && /^\d+$/.test(turnover)==false){
					layer.msg("周期余量只能是正整数");
					return false;
				}
				var temp3 = $("#dailyCapacity").val();
				if(temp3!="" && /^\d+$/.test($("#dailyCapacity").val())==false){
					layer.msg("日产能只能是正整数");
					return false;
				}
				var lossRate=$("#lossRate").val();
				if( ""==lossRate || isNaN(lossRate) ){
					layer.msg("损耗率只能是数字");
					return false;
				}
				if(lossRate<0 || lossRate >100){
					layer.msg("请输入0-100的损耗率");
					return false;
				}
				ajaxUtil.ajaxRuquest(
						"basicData/insertCycle",
						page.insertParam(),
						function(data){
							//点击提交按钮后刷新表
							page.getProductionLine('1');
							page.getAdminWorkshop();
							dataTable.ajax.reload(null,false);
							layAlert("插入成功");
						}
				);
			});
			//点击修改按钮时
			$("#updateBtn").bind("click",function(){
				var id = $(this).val();
				console.log(id);
				if(id==0 || id==""){
					layAlert("请点击添加按钮");
					return false;
				}
				var productionLine = $("#productionLine option:selected").text();
				if(productionLine=="请选择"){
					layAlert("请选择生产线");
					return false;
				}
				var workshopName = $("#workshopName option:selected").text();
				if(workshopName=="请选择"){
					layAlert("请选择车间名称");
					return false;
				}
				var status = $("#status").val();
				if(status=="请选择"){
					layAlert("请选择状态");
					return false;
				}
				processingObject = "";
				$("#processingObject .pad").each(function(){
					processingObject+=$(this).attr("value")+",";
				});
				processingObject = processingObject.substring(0,processingObject.length-1);
				var sort = $("#orderId").val();
				if(sort!="" && /^\d+$/.test(sort)==false){
					layer.msg("顺序只能是正整数");
					return false;
				}
				var temp1 = $("#processCycle").val();
				if(temp1!="" && /^\d+$/.test(temp1)==false){
					layer.msg("流程周期只能是正整数");
					return false;
				}
				var temp2 = $("#repairCycle").val();
				if(temp2!="" && /^\d+$/.test(temp2)==false){
					layer.msg("返修周期只能是正整数");
					return false;
				}
				var turnover = $("#turnover").val();
				if(turnover!="" && /^\d+$/.test(turnover)==false){
					layer.msg("周期余量只能是正整数");
					return false;
				}
				var temp3 = $("#dailyCapacity").val();
				if(temp3!="" && /^\d+$/.test($("#dailyCapacity").val())==false){
					layer.msg("日产能只能是正整数");
					return false;
				}
				var lossRate=$("#lossRate").val();
				if(isNaN(lossRate)){
					layer.msg("损耗率只能是数字");
					return false;
				}
				if(lossRate<0 || lossRate >100){
					layer.msg("请输入0-100的损耗率");
					return false;
				}
				ajaxUtil.ajaxRuquest(
						"basicData/updateCycle",
						page.updateParam(),
						function(data){
							//点击提交按钮后刷新表
							page.getProductionLine('1');
							page.getAdminWorkshop();
							dataTable.ajax.reload(null,false);
							layAlert("修改成功");
						}
				);
			});
		},
		//获取admin公司的车间信息
		getAdminWorkshop:function(){
			 ajaxUtil.ajaxRuquest("basicData/getAdminWorkshop",{},function(data){
				$(".left-box2").html("");
				for(var i=0,l=data.length;i<l;i++){
					$(".left-box2").append("<li class='item ui-sortable-handle' value='"+data[i].id+"' >"+data[i].productionLine+" "+data[i].workshopName+"</li>");
				}
				page.leftAndRightBind2();
			});
		},
		leftAndRightBind2:function(){
			$(".item").unbind("click");
			$(".left-box2 .item").unbind("dblclick");
			$(".right-box2 .item").unbind("dblclick");
			//单击
			$(".item").bind("click",function(){
				$(this).toggleClass("selected-item");
			});
			//左边双击右移
			$(".left-box2 .item").bind("dblclick",function(){
				$(".right-box2").append($(this).removeClass('selected-item'));
				page.leftAndRightBind2();
			});
            //右边双击左移
			$(".right-box2 .item").bind("dblclick",function(){
				$(".left-box2").append($(this).removeClass('selected-item'));
				page.leftAndRightBind2();
			});
		},
		//获取物料表物料类型字段  加工对象
		getMaterialType:function(){
			ajaxUtil.ajaxRuquest("basicData/getRowMaterialType",
					{"isvolid":1},
					function(data){
				var materialTypes = "<li class='item ui-sortable-handle' value='整架' >整架</li>";
				for(var i in data){
					materialTypes+="<li class='item ui-sortable-handle' value='"+data[i]+"' >"+data[i]+"</li>";
				}
				$(".left-box").html("");
				$(".left-box").html(materialTypes);
				page.leftAndRightBind();
			});
		},
		leftAndRightBind:function(){
			$(".item").unbind("click");
			$(".left-box .item").unbind("dblclick");
			$(".right-box .item").unbind("dblclick");
			//单击
			$(".item").bind("click",function(){
				$(this).toggleClass("selected-item");
			});
			//左边双击右移
			$(".left-box .item").bind("dblclick",function(){
				$(".right-box").append($(this).removeClass('selected-item'));
				page.leftAndRightBind();
			});
            //右边双击左移
			$(".right-box .item").bind("dblclick",function(){
				$(".left-box").append($(this).removeClass('selected-item'));
				page.leftAndRightBind();
			});
		},
		removeBtnBind:function(){
			$(".add-one").bind("click",function(){
				$(".right-box").append($(".left-box").find('.selected-item').removeClass('selected-item'));
			});
			$(".add-all").bind("click",function(){
				$(".right-box").append($(".left-box").find('.item').removeClass('selected-item'));
			});
			$(".remove-one").bind("click",function(){
				$(".left-box").append($(".right-box").find('.selected-item').removeClass('selected-item'));
			});
			$(".remove-all").bind("click",function(){
				$(".left-box").append($(".right-box").find('.item').removeClass('selected-item'));
			});
			$(".add-one2").bind("click",function(){
				$(".right-box2").append($(".left-box2").find('.selected-item').removeClass('selected-item'));
			});
			$(".add-all2").bind("click",function(){
				$(".right-box2").append($(".left-box2").find('.item').removeClass('selected-item'));
			});
			$(".remove-one2").bind("click",function(){
				$(".left-box2").append($(".right-box2").find('.selected-item').removeClass('selected-item'));
			});
			$(".remove-all2").bind("click",function(){
				$(".left-box2").append($(".right-box2").find('.item').removeClass('selected-item'));
			});
		},
		//获取生产线填入productionLine控件中
		getProductionLine:function(view){
			ajaxUtil.ajaxRuquest("basicData/getDeptName",
					{'view':view},function(data){
				var html = "<option>请选择</option>";
				for(var i=0;i<data.length;i++){
					html = html+"<option value='"+data[i].id+"'>"+data[i].deptName+"</option>"
				}
				$("#productionLine").html("");
				$("#productionLine").append(html);
			});
		},
		getWorkshopName2:function(deptId){
			console.log("deptId:"+deptId);
			return ajaxUtil.ajaxRequest('basicData/getWorkshopName2',{'deptId':deptId,'view':$("#view").val()}).then(function(data){
				var html = "<option>请选择</option>";
				for(var i=0;i<data.length;i++){
					html = html+"<option value='"+data[i].id+"'>"+data[i].workshopName+"</option>"
				}
				$("#workshopName").html("");
				$("#workshopName").append(html);
				return data;
			}).catch(function(){
				console.log("catch getWorkshopName");
				return;
			});
		},
		//获取车间生产周期表详情
		getWorkshopProductionDetail:function(id){
			ajaxUtil.ajaxRuquest(
					'workshop/getWorkshopProductionDetail',
					{
						'id':id
					},
					function(data){
						if(data!=""){
							//当返回数据不为空时
							$("#updateBtn").val(data[0].id);
							$('#productionLine').val(data[0].productionLine);
							page.getWorkshopName2(data[0].productionLine).then(function(){
								$('#workshopName').val(data[0].id);
							}).catch(function(){
								console.log("catch getWorkshopProductionDetail");
							});
							$("#orderId").val(data[0].orderId);
							$("#status").val(data[0].status);
							var processingObjectHtml = "";
							var processingObjectArr = data[0].processingObject.split(",");
							//当split一个空字符串时，length将会为1
							if(processingObjectArr.length==1){
								if(processingObjectArr[0]==""){
									//为空字符串
									$("#processingObject").html("<button class='btn btn-primary radius'>选择加工对象</button>");
								}else{
									//表示有一个加工对象
									processingObjectHtml+="<button class='btn btn-success radius pad' value='"+processingObjectArr[0]+"'>"+processingObjectArr[0]+"</button>&nbsp;";
									$("#processingObject").html(processingObjectHtml);
								}
							}else{
								//length>1
								for(var i=0,leng=processingObjectArr.length;i<leng;i++){
									processingObjectHtml+="<button class='btn btn-success radius pad' value='"+processingObjectArr[i]+"'>"+processingObjectArr[i]+"</button>&nbsp;";
								}
								$("#processingObject").html(processingObjectHtml);
							}
							$("#creater").val(data[0].creater);
							$("#creationTime").val(data[0].creationTime);
							$("#lastModifyTime").val(data[0].lastModifyTime);
							$("#lastModifier").val(data[0].lastModifier);
							$("#processCycle").val(data[0].processCycle);
							$("#repairCycle").val(data[0].repairCycle);
							$("#turnover").val(data[0].turnover);
							$("#dailyCapacity").val(data[0].dailyCapacity);
							$("#lossRate").val(data[0].lossRate);
//							$("#workshopProductionCycleCode").val(data[0].workshopProductionCycleCode);
//							$("#workshopProductionCycleName").val(data[0].workshopProductionCycleName);
						}
					}
			);
		},
		//设置insert事件传入的参数
		insertParam:function(){
			var productionLine = $("#productionLine option:selected").text();
			var deptId = $("#productionLine").val();
			var workshopName = $("#workshopName option:selected").text();
			var orderId = $("#orderId").val();
			orderId = orderId==""?0:orderId;
			var status = $("#status").val();
			var processCycle = $("#processCycle").val();
			processCycle = processCycle==""?0:processCycle;
			var repairCycle = $("#repairCycle").val();
			repairCycle = repairCycle==""?0:repairCycle;
			var turnover = $("#turnover").val();
			turnover = turnover==""?0:turnover;
			var dailyCapacity = $("#dailyCapacity").val();
			dailyCapacity = dailyCapacity==""?0:dailyCapacity;
			var workshopProductionCycleCode = $("#workshopProductionCycleCode").val();
			var workshopProductionCycleName = $("#workshopProductionCycleName").val();
			var lossRate=$("#lossRate").val();
			var sendData = {
					'productionLine':productionLine,
					'deptId':deptId,
					'workshopName':workshopName,
					'orderId':orderId,
					'status':status,
					'processingObject':processingObject,
					'processCycle':processCycle,
					'dailyCapacity':dailyCapacity,
					'workshopProductionCycleCode':workshopProductionCycleCode,
					'workshopProductionCycleName':workshopProductionCycleName,
					'repairCycle':repairCycle,
					'turnover':turnover,
					"lossRate":lossRate
			}
			return sendData;
		},
		//设置update事件传入的参数
		updateParam:function(){
			var id = $("#workshopName").val();
			var productionLine = $("#productionLine").val();
			var workshopName = $("#workshopName option:selected").text();
			var orderId = $("#orderId").val();
			var status = $("#status").val();
			var lastModifyTime = $("#lastModifyTime").val();
			var lastModifier = $("#lastModifier").val();
			var processCycle = $("#processCycle").val();
			var repairCycle = $("#repairCycle").val();
			var turnover = $("#turnover").val();
			var dailyCapacity = $("#dailyCapacity").val();
			var workshopProductionCycleCode = $("#workshopProductionCycleCode").val();
			var workshopProductionCycleName = $("#workshopProductionCycleName").val();
			var lossRate=$("#lossRate").val();
			var sendData = {
					'id':id,
					'productionLine':productionLine,
					'workshopName':workshopName,
					'orderId':orderId,
					'status':status,
					'processingObject':processingObject,
					'lastModifyTime':lastModifyTime,
					'lastModifier':lastModifier,
					'processCycle':processCycle,
					'dailyCapacity':dailyCapacity,
					'workshopProductionCycleCode':workshopProductionCycleCode,
					'workshopProductionCycleName':workshopProductionCycleName,
					'repairCycle':repairCycle,
					'turnover':turnover,
					"lossRate":lossRate
			}
			return sendData;
		}
}

var tableData=[];//保存查询周期结果
//查询所有周期
var dataTable;
function queryCycle(view){
	$('#queryCycleTable tbody').unbind("click");
	dataTable = ajaxUtil.dataTablesAjax("queryCycleTable",10,"basicData/queryCycle",
			{'view':view},[
				{"data":"productionLine"},
				{"data":"workshopName"},
				{"data":"status"}
			]);
	//行点击事件
	$('#queryCycleTable tbody').on('click', 'tr', function () {
        var data = dataTable.row(this).data();
        var id = data.id;
        var deptId = data.deptId;
        var productionLine = data.productionLine;
        var workshopName = data.workshopName;
        
        page.getWorkshopProductionDetail(id);
    });
}
